package com.lhh.myspringboot.interfac;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 参数检验工厂类  工厂模式实现的
 */

@Component
public class CheckParamFactory  implements ApplicationContextAware {


    public static Map<CheckParamEnum, CheckParam> checkParamBeanMap ;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, CheckParam> map = applicationContext.getBeansOfType(CheckParam.class);
        System.out.println("map:" + JSON.toJSONString(map));
        checkParamBeanMap = new HashMap<>();
        map.forEach((key, value) -> checkParamBeanMap.put(value.getCode(), value));

        System.out.println("checkParamBeanMap:" + JSON.toJSONString(checkParamBeanMap));
    }

    public static <T extends CheckParam> T getCheckParamBean(CheckParamEnum code) {
        return (T)checkParamBeanMap.get(code);
    }


}
